package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.ProductEntityMapper;
import com.showcai.module.BaseModule;
import com.showcai.module.ProductEntity;
import com.showcai.module.User;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.ProductEntityService;
import com.showcai.service.UserService;
import com.showcai.util.GenerateID;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zpc on 2017/3/31.
 */
@Service("productEntityService")
public class ProductEntityServiceImpl extends ProductEntityBase implements ProductEntityService {
    @Autowired
    ProductEntityMapper productEntityMapper;

    @Autowired
    UserService userService;


    @Override
    public void deleteProductEntity(List<Long> productEntityIdList) {
        super.deleteProductEntity(productEntityIdList);
    }

    @Override
    @Transactional
    public ProductEntity modifyProductEntity(ProductEntity productEntity) {


        if (productEntity.getTid() == null) {
            productEntity.setProductEntityId(GenerateID.CreatetID());
            productEntity.setCreateTime(new Date());
            System.out.println("添加的产品实体————————————"+productEntity);
            super.insertProductEntity(productEntity);
        } else {
            super.updateProductEntity(productEntity);
        }

        return productEntity;
    }

    @Override
    public void stopAllProductEntity(Long productId) {
        ProductEntity productEntity = new ProductEntity();
        productEntity.setStatus(BaseModule.STATUS_UNVALID);
        productEntity.setProductId(productId);
        productEntityMapper.updateProductEntityById(productEntity);
    }

    @Override
    public void operateAllProductEntity(Long productId) {
        ProductEntity productEntity = new ProductEntity();
        productEntity.setStatus(BaseModule.STATUS_VALID);
        productEntity.setProductId(productId);
        productEntityMapper.updateProductEntityById(productEntity);
    }

    @Override
    public void stopProductEntity(Long tid) {
        ProductEntity productEntity = new ProductEntity();
        productEntity.setTid(tid);
        productEntity.setStatus(BaseModule.STATUS_UNVALID);
        productEntityMapper.updateProductEntityById(productEntity);
    }

    @Override
    public void operateProductEntity(Long tid) {
        ProductEntity productEntity = new ProductEntity();
        productEntity.setTid(tid);
        productEntity.setStatus(BaseModule.STATUS_VALID);
        productEntityMapper.updateProductEntityById(productEntity);
    }


    @Override
    public List<ProductEntity> getProductEntityListByProductId(Long productId) {
        return productEntityMapper.getProductEntityListByProductId(productId);
    }

    @Override
    public List<ProductEntity> getProductEntityList(Map<String, String> map) throws MyRunTimeExcption {
        return productEntityMapper.getProductEntityList(map);
    }

    @Override
    public PageInfo getProductEntityPageList(Map<String, String> map) throws MyRunTimeExcption {
        Integer page = Integer.parseInt(map.get("page"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        Long productId = Long.valueOf(map.get("productId"));
        PageHelper.startPage(page,pageSize);
        List<ProductEntity> list = productEntityMapper.getProductEntityListByProductId(productId);
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public ProductEntity getProductEntityById(Long productEntityId) {
        return productEntityMapper.getProductEntityByProductEntityId(productEntityId);
    }

    @Override
    public ProductEntity getProductEntity(Map<String, String> map) {
        ProductEntity productEntity = productEntityMapper.getProductEntity(map);
        String activityId = map.get("activityId");
        String openId=map.get("openId");
        if(StringUtils.isBlank(openId)){
            logger.info("openID is  null");

                String userId=map.get("userId");
            if(StringUtils.isNotBlank(userId)){
                logger.info("userId is  null");
                User user=userService.getUserById(Long.parseLong(userId));
            }
        }
        return productEntity;
    }


    @Override
    public PageInfo getPageInfoProductEntityList(Map<String, String> map) throws MyRunTimeExcption {
        Integer page = Integer.parseInt(map.get("page"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(page, pageSize);
        List<ProductEntity> productEntityList = productEntityMapper.getProductEntityList(map);

        PageInfo<ProductEntity> pageinfo = new PageInfo<>(productEntityList);
        return pageinfo;
    }
}
